package com.ruoyi.common.util;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.entity.LockDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class LockDocUtils {
    @Autowired
    private RedisCache redisCache;

    /* *
     *
     * @param docUnid
     * @param user
     * @return  Boolean true：成功，false：失败
     */
    public Boolean lockDoc(String docUnid, SysUser user){
        //默认状态未 未成功
        Boolean flag = false;
        Map<String,Object> returnMap = new HashMap<>();
        String redisKey = "lock_doc_" + docUnid;
        LockDoc lockDoc = new LockDoc();
        lockDoc.setId(Tools.createUUID());
        lockDoc.setDocUnid(docUnid);
        lockDoc.setUser(user);
        lockDoc.setCreateDate(Tools.getCurrentDateTime());
        LockDoc lockDocRedisCache = redisCache.getCacheObject(redisKey);
        try {
            if (lockDocRedisCache != null){
                //如果有记录表示正在锁定中
                //如果是同一个人新增锁
                if(lockDocRedisCache.getUser().getUserId() == user.getUserId()){
                    //更新锁的生命周期
                    redisCache.expire(redisKey,30*60);
                    flag = true;
                }
            }else{
                //如果没找到，新增lock_doc redis缓存
                redisCache.setCacheObject(redisKey,lockDoc);
                //设置锁文档时间为30分钟
                redisCache.expire(redisKey,30*60);
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    public Boolean unlockDocs(String docUnid, SysUser user){
        String  redisKey = "lock_doc_" + docUnid;
        LockDoc lockDocRedisCache = redisCache.getCacheObject(redisKey);
        if (lockDocRedisCache == null){
            return false;
        }else{
            //如果上锁和解锁是同一个人，那么进行解锁操作
            if (lockDocRedisCache.getUser().getUserId() == user.getUserId()){
                Boolean flag = redisCache.deleteObject(redisKey);
                if (flag) return true;
            }
        }
        return false;
    }

    /**
     *
     * @param docUnid 文档id
     * @param user 当前用户user对象
     * @return Map<String,Object>
     *     flag：true 表示文档正在被锁定
     *     lockUser：正在锁定文档的user对象，只有true时候才会返回
     */
    public Map<String,Object> checkIsLocked(String docUnid, SysUser user){
        Map<String,Object> returnMap = new HashMap<>();
        boolean flag = true;
        String  redisKey = "lock_doc_" + docUnid;
        LockDoc lockDocRedisCache = redisCache.getCacheObject(redisKey);
        System.out.println("lockDocRedisCache======="+lockDocRedisCache);
        if (lockDocRedisCache == null){
            //如果为空 表示未锁
            flag = false;
        }else{
            //如果是同一个人
            if (lockDocRedisCache.getUser().getUserId() == user.getUserId()){
                flag = false;
            }else{
                flag = true;
                returnMap.put("lockUser",lockDocRedisCache.getUser());
            }
        }
        returnMap.put("flag",flag);
        return returnMap;
    }
}
